﻿using BCCommon.PaymentEnums;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BCService.Common.Payment.Payment.WeixinPay
{
    /// <summary>
    /// 微信支付配置内部类
    /// </summary>
    public class WeiXinPayPlugConfig : IWeiXinPayPlugConfig
    {
        /// <summary>
        /// 是否初始化完成
        /// </summary>
        public bool initDone { get; set; }

        public string APP_ID { get; set; }

        public string APP_SECRET { get; set; }

        //商户号
        public string MCH_ID { get; set; }

        /// 密钥  
        public string KEY { get; set; }

        /// <summary>
        /// 证书路径
        /// </summary>
        public string CERT_PATH { get; set; }

        /// <summary>
        /// 证书密码
        /// </summary>
        public string CERT_PASSWORD { get; set; }

        /// <summary>
        /// 随机字符串（32位最长）
        /// </summary>
        public string NONCE_STR { get; set; }


        private readonly IConfiguration _configuration;

        public WeiXinPayPlugConfig(IConfiguration configuration)
        {
            _configuration = configuration;
            initDone = false;
        }
        public void InitConfig(SourceAppType? appType, string appId = null)
        {
            if (appType.HasValue)
            {
                APP_ID = _configuration[$"WeixinPay:{appType.ToString()}:APP_ID"];
                MCH_ID = _configuration[$"WeixinPay:{appType.ToString()}:MCH_ID"];
                KEY = _configuration[$"WeixinPay:{appType.ToString()}:KEY"];
                APP_SECRET = _configuration[$"WeixinPay:{appType.ToString()}:App_Secret"];
                //证书路径

                //CERT_PATH = Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, _configuration[$"WeixinPay:{appType.ToString()}:Cert_Path"]);

                CERT_PATH = Path.Combine("/etc/nginx/", _configuration[$"WeixinPay:{appType.ToString()}:Cert_Path"]);

                CERT_PASSWORD = MCH_ID;
            }
            
            //if (appType == SourceAppType.WebsiteApp || appId == _configuration[$"WeixinPay:{appType.ToString()}:APP_ID"])
            //{

            //}

            //if (appType == SourceAppType.ClientApp || appId == _configuration["WeixinPay:ClientApp:APP_ID"])
            //{
            //    APP_ID = _configuration["WeixinPay:ClientApp:APP_ID"];
            //    MCH_ID = _configuration["WeixinPay:ClientApp:MCH_ID"];
            //    KEY = _configuration["WeixinPay:ClientApp:KEY"];
            //    APP_SECRET = _configuration["WeixinPay:ClientApp:App_Secret"];
            //    //证书路径
            //    CERT_PATH = Path.Combine("/etc/nginx/", "apiclient_cert.p12");
            //    //Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, GeneralContainer.Configuration["WeixinPay:ClientApp:Cert_Path"]);
            //    CERT_PASSWORD = MCH_ID;
            //}

            //if (appType == SourceAppType.DriverApp || appId == GeneralContainer.Configuration["WeixinPay:DriverApp:APP_ID"])
            //{
            //    APP_ID = GeneralContainer.Configuration["WeixinPay:DriverApp:APP_ID"];
            //    MCH_ID = GeneralContainer.Configuration["WeixinPay:DriverApp:MCH_ID"];
            //    KEY = GeneralContainer.Configuration["WeixinPay:DriverApp:KEY"];
            //    APP_SECRET = GeneralContainer.Configuration["WeixinPay:DriverApp:App_Secret"];
            //    //证书路径
            //    CERT_PATH = Path.Combine("/etc/nginx/", "apiclient_cert.p12");
            //    //Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, GeneralContainer.Configuration["WeixinPay:DriverApp:Cert_Path"]);
            //    CERT_PASSWORD = MCH_ID;
            //}

            //if (appType == SourceAppType.PartnerApp || appId == GeneralContainer.Configuration["WeixinPay:PartnerApp:APP_ID"])
            //{
            //    APP_ID = GeneralContainer.Configuration["WeixinPay:PartnerApp:APP_ID"];
            //    MCH_ID = GeneralContainer.Configuration["WeixinPay:PartnerApp:MCH_ID"];
            //    KEY = GeneralContainer.Configuration["WeixinPay:PartnerApp:KEY"];
            //    APP_SECRET = GeneralContainer.Configuration["WeixinPay:PartnerApp:App_Secret"];
            //    //证书路径
            //    CERT_PATH = Path.Combine("/etc/nginx/", "apiclient_cert.p12");
            //    //Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, GeneralContainer.Configuration["WeixinPay:PartnerApp:Cert_Path"]);
            //    CERT_PASSWORD = MCH_ID;
            //}

            if (!appType.HasValue && appId == null)
            {
                APP_ID = _configuration["WeixinPay:WebsiteApp:APP_ID"];
                MCH_ID = _configuration["WeixinPay:WebsiteApp:MCH_ID"];
            }
            initDone = true;
        }
    }
}
